package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.ArticleTagDto;
import com.example.entity.*;
import com.example.service.*;
import com.example.mapper.ArticleMapper;
import com.example.utils.BeanCopyUtils;
import com.example.utils.ResponseResult;
import com.example.utils.SecurityUtils;
import com.example.utils.SystemConstants;
import com.example.vo.ArticleListVo;
import com.example.vo.HotArticleVo;
import com.example.vo.PageVo;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author sprite-pc
* @description 针对表【sg_article(文章表)】的数据库操作Service实现
* @createDate 2023-12-16 20:57:41
*/
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService{

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ArticleTagService articleTagService;

    @Autowired
    private CommentService commentService;

    @Override
    public ResponseResult hotArticleList() {
        //查询热门文章 封装成ResponseResult返回
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //必须是正式文章
        queryWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        //按照浏览量进行排序
        queryWrapper.orderByDesc(Article::getViewCount);
        //最多只查询10条
        Page<Article> page = new Page(1,10);
        page(page,queryWrapper);

        List<Article> articles = page.getRecords();

        List<HotArticleVo> vs = BeanCopyUtils.copyBeanList(articles, HotArticleVo.class);

        return ResponseResult.okResult(vs);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId, String title, String summary) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        if (categoryId!=0L){
            queryWrapper.eq(Article::getCategoryId,categoryId);
        }
        if (title != null){
            queryWrapper.like(Article::getTitle,title);
        }
        if (summary != null){
            queryWrapper.like(Article::getSummary,summary);
        }

        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.orderByDesc(Article::getIsTop);

        Page<Article> pageInfo = new Page<>(pageNum,pageSize);
        page(pageInfo, queryWrapper);

        List<Article> articles = pageInfo.getRecords();

        articles = articles.stream().
                map(article -> {
                    article.setCategoryName(categoryService.getById(article.getCategoryId()).getName());
                    return article;
                }).collect(Collectors.toList());

        pageInfo.setRecords(articles);
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(pageInfo.getRecords(), ArticleListVo.class);

        PageVo pageVo = new PageVo(articleListVos, pageInfo.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        if (categoryId!=0L){
            queryWrapper.eq(Article::getCategoryId,categoryId);
        }

        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        queryWrapper.orderByDesc(Article::getIsTop);

        Page<Article> pageInfo = new Page<>(pageNum,pageSize);
        page(pageInfo, queryWrapper);

        List<Article> articles = pageInfo.getRecords();

        articles = articles.stream().
                map(article -> {
                    article.setCategoryName(categoryService.getById(article.getCategoryId()).getName());
                    return article;
                }).collect(Collectors.toList());

        pageInfo.setRecords(articles);
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeanList(pageInfo.getRecords(), ArticleListVo.class);

        PageVo pageVo = new PageVo(articleListVos, pageInfo.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getArticle(int id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult updateViewCount(int id) {
        Article article = getById(id);
        article.setViewCount(article.getViewCount()+1);
        updateById(article);
        return ResponseResult.okResult();
    }

    @Override
    public void publish(Article article) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        article.setCreateBy(loginUser.getUser().getId());
        article.setCreateTime(new Date());
        save(article);
    }

    @Override
    public ResponseResult getList(int pageNum, int pageSize) {
        Page<Article> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Article> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getCreateBy,SecurityUtils.getLoginUser().getUser().getId());
        page(pageInfo,queryWrapper);
        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public ResponseResult updateArticle(ArticleTagDto article) {
        List<String> tagList= article.getTags();
        LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleTag::getArticleId,article.getId());
        articleTagService.remove(queryWrapper);
        if (tagList!=null){
            for (String tagId: tagList){
                ArticleTag articleTag = new ArticleTag();
                articleTag.setArticleId(article.getId());
                articleTag.setTagId(new Long(tagId));
                articleTagService.save(articleTag);
            }
        }
        Article article1 = BeanCopyUtils.copyBean(article,Article.class);
        updateById(article1);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteArticle(List<Long> ids) {
        removeByIds(ids);
        for (Long id:ids){
            LambdaQueryWrapper<ArticleTag> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ArticleTag::getArticleId,id);
            articleTagService.remove(queryWrapper);

            LambdaQueryWrapper<Comment> queryWrapper_Comment = new LambdaQueryWrapper<>();
            queryWrapper_Comment.eq(Comment::getArticleId,id);
            commentService.remove(queryWrapper_Comment);
        }

        return ResponseResult.okResult();
    }
}




